<template>
  <div class="attr-manage">
    <el-dialog :title="dataForm.attrId ? '编辑' : '新增'" :close-on-click-modal="false" :destroy-on-close="true"
      :visible.sync="visible" width="750px" @close="closeDialog">
      <el-form ref="dataForm" :rules="rules" :model="dataForm" label-position="left" label-width="130px"
        style="min-width: 600px; width: 600px; margin-left:35px;">
        <!-- 属性名称 -->
        <el-form-item label="属性名称" prop="name">
          <el-input v-model="dataForm.name" maxlength="20" show-word-limit placeholder="请输入属性名称" />
        </el-form-item>
        <el-form-item label="属性类型">
          <el-radio v-model="dataForm.attrType" :label="1">基本属性</el-radio>
          <el-radio v-model="dataForm.attrType" :label="0">销售属性</el-radio>
        </el-form-item>
        <el-form-item label="是否需要被检索">
          <el-radio v-model="dataForm.searchType" :label="0">不需要</el-radio>
          <el-radio v-model="dataForm.searchType" :label="1">需要</el-radio>
        </el-form-item>
        <el-form-item label="所属分类">
          <el-select v-model="currentSelectCategory" placeholder="请选择">
            <el-option v-for="item in categoryList" :key="item.categoryId" :label="item.name" :value="item.categoryId">
            </el-option>
          </el-select>
          <el-button icon="el-icon-plus" class="add-btn" @click="addCategory">添加</el-button>
          <div>
            <span>已选择：</span>
            <el-tag v-for="(category, index) in selectedCategorys" :key="category.categoryId" closable type="success"
              @close="removeCategory(index)">
              {{ category.name }}
            </el-tag>
          </div>

        </el-form-item>
        <!-- 属性描述 -->
        <el-form-item label="属性描述" prop="desc">
          <el-input v-model="dataForm.desc" type="textarea" :rows="2" maxlength="30" show-word-limit
            placeholder="请输入新增属性的相关描述，30字以内" />
        </el-form-item>
        <el-form-item label="属性值" prop="attrValues">
          <div class="attr-int">
            <el-input v-model="attrValue" clearable maxlength="20" show-word-limit placeholder="请输入要添加的属性值，20字以内" />
            <el-button icon="el-icon-plus" class="add-btn" @click="addAttrValue(attrValue)">添加</el-button>
          </div>
          <div v-if="dataForm.attrValues.length" class="attr-val">
            <div class="attr-scr">
              <div v-for="(attr, idx) in dataForm.attrValues" :key="attr.attrValueId"
                :class="['attr-val-item', attr.error ? 'error' : '']">
                <el-input v-model="attr.value" maxlength="20" show-word-limit placeholder="请输入要添加的属性值，20字以内"
                  @blur="checkIntValue(idx)" />
                <el-button type="text" class="txt-del-btn" @click="deleteAttrCalue(idx)">删除</el-button>
              </div>
            </div>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dataFormSubmit()">
          提交
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as api from '@/api/product/attr'
import { platform_first_category } from '@/api/product/category'
export default {
  components: {},
  data() {
    return {
      visible: false,
      dataForm: {
        attrId: null,
        attrType: 0, // 属性类型 0:销售属性，1:基本属性
        attrValues: [], // 属性值列表
        categoryIds: [], // 分类id列表
        desc: '', // 属性描述
        name: '', // 属性名称
        searchType: 1 // 作为搜索参数 0:不需要，1:需要
      },
      rules: {
        name: [
          { required: true, message: '请输入属性名称', trigger: 'blur' }
        ]
      },
      attrValue: null,
      selectedCategorys: [],
      currentSelectCategory: null,
      categoryList: [], // 分类列表
    }
  },
  computed: {
    categoryMap() {
      let map = {}
      this.categoryList.forEach(item => {
        map[item.categoryId] = item
      })
      return map
    },
  },
  mounted() {
  },
  created() {
  },
  methods: {
    init(attrId) {
      this.dataForm.attrId = attrId || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        platform_first_category().then(res => {
          this.categoryList = res.data
          this.currentSelectCategory = this.categoryList[0].categoryId
          if (this.dataForm.categoryIds.length) {
            this.dataForm.categoryIds.forEach(item => {
              this.selectedCategorys.push(this.categoryMap[item])
            })
          }
        })
        if (!this.dataForm.attrId) {
          this.attrValue = null
          this.dataForm.attrValues = []
          this.dataForm.categoryIds = []
          this.selectedCategorys = []
          return
        }
        api.get(attrId).then(res => {
          this.dataForm = res.data
          this.dataForm.attrValues = res.data.attrValues || []
          this.selectedCategorys = res.data.categories || []
          this.dataForm.categoryIds = this.selectedCategorys.map(item => item.categoryId) || []
        })

      })
    },
    addCategory() {
      let item = this.categoryMap[this.currentSelectCategory]
      this.selectedCategorys.forEach((el, index) => {
        if (el.categoryId === this.currentSelectCategory) {
          this.selectedCategorys.splice(index, 1)
          this.dataForm.categoryIds.splice(index, 1)
        }
      })
      this.selectedCategorys.push(item)
      this.dataForm.categoryIds.push(item.categoryId)
    },
    removeCategory(index) {
      this.selectedCategorys.splice(index, 1)
      this.dataForm.categoryIds.splice(index, 1)
    },
    // 关闭dialog时
    closeDialog() {
      this.dataForm = {
        attrId: null,
        attrValues: [], // 属性值列表
        desc: '', // 属性描述
        name: '', // 属性名称
        attrType: 0, // 属性类型 0:销售属性，1:基本属性
        categoryIds: [], // 分类id列表
        searchType: 1 // 作为搜索参数 0:不需要，1:需要
      }
      this.selectedCategorys = []
    },

    /**
     * 添加属性值
     */
    addAttrValue(attrVal) {
      if (!attrVal) {
        return
      }
      this.dataForm.attrValues.forEach((el, idx) => {
        if (el.value === attrVal) {
          this.dataForm.attrValues.splice(idx, 1)
        }
      })
      this.dataForm.attrValues.push({ attrValueId: null, attrId: null, value: attrVal })
      this.attrValue = ''
      // console.log('添加属性值dataForm.attrValues：', this.dataForm.attrValues)
    },

    /**
     * 删除属性值
     */
    deleteAttrCalue(idx) {
      this.dataForm.attrValues.splice(idx, 1)
      // console.log('删除属性值dataForm.attrValues：', this.dataForm.attrValues)
    },

    /**
     * 失焦时属性值是否为空
     */
    checkIntValue(idx) {
      if (!this.dataForm.attrValues[idx].value) {
        this.$message({
          message: '已创建的属性值不能为空！请重新创建',
          type: 'warining',
          duration: 1500
        })
        this.dataForm.attrValues.splice(idx, 1)
      }
    },

    /**
     * 表单提交
     */
    dataFormSubmit() {
      this.$refs.dataForm.validate(valid => {
        if (!valid) {
          return
        }
        if (!this.dataForm.categoryIds.length) {
          this.$message({
            message: '请至少选择一个分类',
            type: 'warining',
            duration: 1500
          })
          return
        }
        if (!this.dataForm.attrValues.length) {
          this.$message({
            message: '请至少创建一个属性值',
            type: 'warining',
            duration: 1500
          })
          return
        }
        const request = this.dataForm.attrId ? api.update(this.dataForm) : api.save(this.dataForm)
        request.then(data => {
          this.$message({
            message: "操作成功",
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
              this.$refs['dataForm'].resetFields()
            }
          })
        })
      })
    }

  }
}
</script>

<style lang="scss">
p {
  margin: 0;
}

.attr-manage {
  .attr-int {
    display: flex;
    align-items: center;
    justify-content: space-between;

    .add-btn {
      margin-left: 10px;
    }

    .add-btn.el-button--medium {
      padding: 10px 10px !important;
    }
  }

  .attr-val {
    background: #f8f8f8;
    margin-top: 20px;
    padding: 20px 0;
    box-sizing: border-box;

    .attr-scr {
      min-height: 40px;
      max-height: 220px;
      overflow-y: auto;
      padding: 0 20px;
    }

    // .attr-scr::-webkit-scrollbar { // 隐藏滚动条
    //   display: none;
    // }
    .attr-val-item {
      display: flex;
      align-items: center;
      justify-content: space-between;
    }

    .attr-val-item:not(:last-child) {
      margin-bottom: 10px;
    }

    .attr-val-item .int-con {
      display: block;
      width: 100%;
    }

    .attr-val-item .txt-del-btn {
      margin-left: 15px;
    }
  }
}
</style>
